package com.github.catvod.spider.merge;

import android.text.TextUtils;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/* loaded from: classes.dex */
class k {
    private String BQ;
    private final String I;
    private final Map<String, String> V0;
    private Object W;
    private final Map<String, List<String>> h;
    private final Map<String, String> j;
    private Request oC;
    private final String q;

    k(String str, String str2, Map<String, String> map, Map<String, String> map2, Map<String, List<String>> map3) {
        this(str, str2, null, map, map2, map3);
    }

    private void V0() {
        Request.Builder builder = new Request.Builder();
        if (this.q.equals("GET") && this.j != null) {
            q();
        }
        if (this.q.equals("POST")) {
            builder.post(j());
        }
        Map<String, String> map = this.V0;
        if (map != null) {
            for (String str : map.keySet()) {
                builder.addHeader(str, this.V0.get(str));
            }
        }
        Object obj = this.W;
        if (obj != null) {
            builder.tag(obj);
        }
        this.oC = builder.url(this.BQ).build();
    }

    private RequestBody j() {
        if (!TextUtils.isEmpty(this.I)) {
            return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), this.I);
        }
        FormBody.Builder builder = new FormBody.Builder(CE.q);
        Map<String, String> map = this.j;
        if (map != null) {
            for (String str : map.keySet()) {
                builder.add(str, this.j.get(str));
            }
        }
        return builder.build();
    }

    private void q() {
        this.BQ += "?";
        for (String str : this.j.keySet()) {
            this.BQ = this.BQ.concat(str + "=" + this.j.get(str) + "&");
        }
        this.BQ = this.BQ.substring(0, r0.length() - 1);
    }

    public String h(OkHttpClient okHttpClient) {
        try {
            Response execute = okHttpClient.newCall(this.oC).execute();
            Map<String, List<String>> map = this.h;
            if (map != null) {
                map.clear();
            }
            Map<String, List<String>> map2 = this.h;
            if (map2 != null) {
                map2.putAll(execute.headers().toMultimap());
            }
            return execute.body().string();
        } catch (IOException unused) {
            return "";
        }
    }

    private k(String str, String str2, String str3, Map<String, String> map, Map<String, String> map2, Map<String, List<String>> map3) {
        this.BQ = str2;
        this.I = str3;
        this.q = str;
        this.j = map;
        this.V0 = map2;
        this.h = map3;
        V0();
    }
}
